<title>轮播图管理</title>

<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <a><cite>调度管理</cite></a>
        <a><cite>轮播管理</cite></a>
    </div>
</div>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="content-banner">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <div class="layui-input-inline">
                        <input type="text" name="name" placeholder="轮播图名称" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-banner" lay-submit lay-filter="LAY-banner-search">
                        搜索
                    </button>
                    <button class="layui-btn banner-btn-common" data-type="add">
                        添加
                    </button>
                </div>
            </div>
        </div>
        <div class="layui-card-body">
            <table id="LAY-banner" lay-filter="LAY-banner"></table>
            <script type="text/html" id="imgTpl">
                <img style="display: inline-block; width: 50%; height: 100%;"
                     src={{ d.imageUrl ? d.imageUrl : "" }} onclick="previewImg(this)">
            </script>
            <script type="text/html" id="table-system-banner">
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i
                        class="layui-icon layui-icon-edit"></i>编辑</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i
                        class="layui-icon layui-icon-delete"></i>删除</a>
            </script>
        </div>
    </div>
</div>


<script>
    function previewImg(obj) {
        var img = new Image();
        img.src = obj.src;
        var imgHtml = "<img src='" + obj.src + "' width='100%' height='100%'/>";
        //弹出层
        layer.open({
            type: 1,
            shade: 0.8,
            offset: 'auto',
            // area: [500 + 'px', 550 + 'px'],
            shadeClose: true,
            scrollbar: false,
            title: "图片预览", //不显示标题
            content: imgHtml, //捕获的元素，注意：最好该指定的元素要存放在body最外层，否则可能被其它的相对元素所影响
            cancel: function () {
                //layer.msg('捕获就是从页面已经存在的元素上，包裹layer的结构', { time: 5000, icon: 6 });
            }
        });
    }
</script>

<script>
    layui.use(['admin', 'table', 'form', 'setter', 'element'], function () {
        var $ = layui.$
            , admin = layui.admin
            , view = layui.view
            , table = layui.table
            , form = layui.form
            , element = layui.element
            , setter = layui.setter;

        var apiUrl = setter.apiUrl;

        table.render({
            elem: '#LAY-banner'
            , url: apiUrl + '/mapi/banner/list'
            , method: 'post'
            , page: true
            , contentType: 'application/json'
            , headers: {'token': layui.data(setter.tableName).token}
            , request: {
                pageName: 'pageNo'
                , limitName: 'pageSize'
            }
            , response: {
                statusCode: 0, //规定数据状态的字段名称，默认：code
            }
            , parseData: function (res) {
                if (res.code === 0) {
                    //将原始数据解析成 table 组件所规定的数据
                    return {
                        "code": res.code, //解析接口状态
                        "msg": res.msg, //解析提示文本
                        "count": res.data.total, //解析数据长度
                        "data": res.data.list //解析数据列表
                    };
                } else {
                    return {
                        "code": res.code, //解析接口状态
                        "msg": res.msg, //解析提示文本
                    };
                }
            }
            , cols: [[
                {field: 'name', title: '轮播图名称'}
                , {field: 'imageUrl', title: '轮播图图片', minWidth: 80, templet: '#imgTpl'}
                , {field: 'backGround', title: '背景色'}
                , {field: 'sort', title: '排序号'}
                , {title: '操作', fixed: 'right', toolbar: '#table-system-banner'}
            ]]
            , text: {
                none: '未检索到数据' //默认：无数据。注：该属性为 layui 2.2.5 开始新增
            }
            , done: function () {
                element.render('progress');
            }
        });

        //监听工具条
        table.on('tool(LAY-banner)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                admin.popup({
                    title: '编辑轮播图'
                    , area: ['420px', '500px']
                    , id: 'LAY-popup-banner-edit'
                    , success: function (layero, index) {
                        view(this.id).render('car/banner/edit', data).done(function () {
                            var bid = data.id;
                            form.render(null, 'form-banner');
                            //监听提交
                            form.on('submit(LAY-banner-edit-submit)', function (data) {
                                //提交 Ajax 成功后，关闭当前弹层并重载表格
                                admin.req({
                                    url: apiUrl + '/mapi/banner/update/' + bid //实际使用请改成服务端真实接口
                                    , method: 'put'
                                    , data: JSON.stringify(data.field)
                                    , done: function (response) {
                                        if (response.code === 0) {
                                            layer.msg('修改成功', {
                                                offset: '15px', icon: 1, time: 1000
                                            }, function () {
                                                layui.table.reload('LAY-banner'); //重载表格
                                                layer.close(index); //执行关闭
                                            });
                                        } else {
                                            layer.msg(response.msg, {
                                                offset: '15px', icon: 2
                                            });
                                        }
                                    }
                                });
                            });
                        });
                    }
                });
            } else if (obj.event === 'del') {
                top.layer.confirm('确定要删除' + data.name + '吗？', {
                        skin: 'layui-layer-admin'
                    }, function (index) {
                        top.layer.close(index);
                        admin.req({
                            url: apiUrl + '/mapi/banner/delete/' + data.id
                            , method: 'delete'
                            , done: function (response) {
                                if (response.code === 0) {
                                    layer.msg('删除成功', {
                                        offset: '15px', icon: 1, time: 1000
                                    }, function () {
                                        layui.table.reload('LAY-banner'); //重载表格
                                    });
                                } else {
                                    layer.msg(response.msg, {
                                        offset: '15px', icon: 2
                                    });
                                }
                            }
                        });
                    }
                );
            }
        });

        //事件
        var active = {
            add: function () {
                admin.popup({
                    title: '添加轮播图'
                    , area: ['420px', '500px']
                    , id: 'LAY-popup-banner-add'
                    , success: function (layero, index) {
                        view(this.id).render('car/banner/add').done(function () {
                            //监听提交
                            form.on('submit(LAY-banner-add-submit)', function (data) {
                                //提交 Ajax 成功后，关闭当前弹层并重载表格
                                admin.req({
                                    url: apiUrl + '/mapi/banner/add' //实际使用请改成服务端真实接口
                                    , method: 'post'
                                    , data: JSON.stringify(data.field)
                                    , done: function (response) {
                                        if (response.code === 0) {
                                            layer.msg('添加成功', {
                                                offset: '15px', icon: 1, time: 1000
                                            }, function () {
                                                layui.table.reload('LAY-banner'); //重载表格
                                                layer.close(index); //执行关闭
                                            });
                                        } else {
                                            layer.msg(response.msg, {
                                                offset: '15px', icon: 2
                                            });
                                        }
                                    }
                                });
                            });
                        });
                    }
                });
            }
        };


        form.render(null, 'content-banner');

        //监听搜索
        form.on('submit(LAY-banner-search)', function (data) {
            var field = data.field;
            //执行重载
            table.reload('LAY-banner', {
                where: field
            });
        });

        $('.layui-btn.banner-btn-common').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>